package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TOssInfo;
import com.ruoyi.system.service.ITOssInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 对象信息Controller
 * 
 * @author ruoyi
 * @date 2024-09-14 17:27:04
 */
@RestController
@RequestMapping("/system/oss")
public class TOssInfoController extends BaseController
{
    @Autowired
    private ITOssInfoService tOssInfoService;

    private static Logger logger = LoggerFactory.getLogger(TOssInfoController.class);

    /**
     * 查询对象信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:oss:list')")
    @GetMapping("/list")
    public TableDataInfo list(TOssInfo tOssInfo)
    {
        startPage();
        List<TOssInfo> list = tOssInfoService.selectTOssInfoList(tOssInfo);
        return getDataTable(list);
    }

    /**
     * 导出对象信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:oss:export')")
    @Log(title = "对象信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TOssInfo tOssInfo)
    {
        List<TOssInfo> list = tOssInfoService.selectTOssInfoList(tOssInfo);
        ExcelUtil<TOssInfo> util = new ExcelUtil<TOssInfo>(TOssInfo.class);
        util.exportExcel(response, list, "对象信息数据");
    }

    /**
     * 获取对象信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:oss:query')")
    @GetMapping(value = "/{ossId}")
    public AjaxResult getInfo(@PathVariable("ossId") Long ossId)
    {
        return success(tOssInfoService.selectTOssInfoByOssId(ossId));
    }

    /**
     * 新增对象信息
     */
    @PreAuthorize("@ss.hasPermi('system:oss:add')")
    @Log(title = "对象信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TOssInfo tOssInfo)
    {
        tOssInfo.setCreateBy(getNickName());
        return toAjax(tOssInfoService.insertTOssInfo(tOssInfo));
    }

    /**
     * 修改对象信息
     */
    @PreAuthorize("@ss.hasPermi('system:oss:edit')")
    @Log(title = "对象信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TOssInfo tOssInfo)
    {
        tOssInfo.setUpdateBy(getNickName());
        return toAjax(tOssInfoService.updateTOssInfo(tOssInfo));
    }

    /**
     * 删除对象信息
     */
    @PreAuthorize("@ss.hasPermi('system:oss:remove')")
    @Log(title = "对象信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ossIds}")
    public AjaxResult remove(@PathVariable Long[] ossIds)
    {
        String uploadPath = RuoYiConfig.getUploadPath();
        //logger.debug("RRR remove = " + uploadPath);
        List<TOssInfo> ossList = tOssInfoService.selectTOssInfoListByOssIds(ossIds);
        for(TOssInfo oss : ossList)
        {
            //logger.debug("RRR oss = " + oss);
            //logger.debug("RRR filePath = " + uploadPath + oss.getFileName().substring(15));
            FileUtils.deleteFile(uploadPath + oss.getFileName().substring(15));
        }
        return toAjax(tOssInfoService.deleteTOssInfoByOssIds(ossIds));
    }
}
